package com.qiezi.project.contract;

import com.alibaba.fastjson.TypeReference;
import com.qiezi.bean.DataBean;
import com.qiezi.bean.MemberTypeBean;
import com.qiezi.bean.ResponseBean;
import com.qiezi.mvp.BaseMvpView;
import com.qiezi.mvp.BasePresenter;
import com.qiezi.util.Request;
import com.zhy.http.okhttp.request.RequestCall;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


public class VipCenterContract {
    public static class Presenter extends BasePresenter<View>{

        private RequestCall call;

        public void loadData(){
            if (call != null)call.cancel();

            call = Request.post(Request.Api.getMemberType, null, new Request.JsonCallback<DataBean<MemberTypeBean>>(new TypeReference<DataBean<MemberTypeBean>>(){}.getType()) {


                @Override
                public void onSueccess(ResponseBean<DataBean<MemberTypeBean>> data) {
                    if (mView == null)return;

                    mView.resultData((ArrayList<MemberTypeBean>) data.getData().getList(),data.getData().isOpen(),data.getData().getEndtime());
                }

                @Override
                public void onFailure(String msg) {
                    if (mView == null)return;
                    mView.showError(msg);
                }

            });
        }

        private Map<String,String> params;

        public void buyMember(String typeId,int payType){
            if (call != null)call.cancel();
            if (params == null){
                params = new HashMap<>(2);
            }
            params.put("type_id",typeId);
            params.put("pay_type",String.valueOf(payType));
            call = Request.post(Request.Api.buyMember, params, new Request.JsonCallback<Object>(Object.class) {
                @Override
                public void onSueccess(ResponseBean data) {
                    if (mView == null)return;
                    mView.onBuyMember();
                }

                @Override
                public void onFailure(String msg) {
                    if (mView == null)return;
                    mView.showError(msg);
                }
            });

        }
    }

    public interface View extends BaseMvpView {
        void resultData(ArrayList<MemberTypeBean> data,boolean isOpen,String endTime);
        void onBuyMember();
    }
}